﻿<?php
Class Question extends CI_Model
{
	function get_question($question_id)
	{
		$this -> db -> select('content, category_id, right_answer, level, note');
		$this -> db -> from('question');
		$this -> db -> where('question_id = ' . "'" . $question_id . "'");
		$this -> db -> limit(1);

		$query = $this -> db -> get();

		if($query -> num_rows() == 1)
		{
			return $query->row();
		}
		else
		{
			return false;
		}
	}
	
	function get_question_by_test($test_id)
	{
		$this -> db -> select('question_id, answer_order');
		$this -> db -> from ('test_question');
		$this -> db -> where('test_id = ' . "'" . $test_id . "'");

		$query = $this -> db -> get();

		if($query -> num_rows() > 0)
		{
			return $query->result();
		}
		else
		{
			return false;
		}	
	}
	
	function get_question_4_test($num_question)
	{
		$num_cate_1 = intval($num_question / 3);
		$num_cate_3 = $num_question - 2 * $num_cate_1;
		
		$this -> db -> select('question_id');
		$this -> db -> from('question');
		$this -> db -> where('category_id = 1 ORDER BY RAND()');
		$this -> db -> limit($num_cate_1);
		
		$query1 = $this -> db -> get();
		
		$this -> db -> select('question_id');
		$this -> db -> from('question');
		$this -> db -> where('category_id = 2 ORDER BY RAND()');
		$this -> db -> limit($num_cate_1);
		
		$query2 = $this -> db -> get();
		
		$this -> db -> select('question_id');
		$this -> db -> from('question');
		$this -> db -> where('category_id = 3 ORDER BY RAND()');
		$this -> db -> limit($num_cate_3);
		
		$query3 = $this -> db -> get();

		if($query1 -> num_rows() == $num_cate_1 && $query2 -> num_rows() == $num_cate_1
			&& $query3 -> num_rows() == $num_cate_3)
		{
			return array_merge($query1->result_array(), $query2->result_array(), $query3->result_array());
		}
		else
		{
			return false;
		}
	}
	
	function insert_question($category_id, $content, $right_answer, $level, $created_user, $note)
	{
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$data = array(
			'category_id' => $category_id,
			'content' => $content,
			'right_answer' => $right_answer,
			'level' => $level,
			'created_user' => $created_user,
			'created_date' => date("Y/m/d H:i:s", time()),
			'modified_user' => $created_user,
			'modified_date' => date("Y/m/d H:i:s", time())
		);
		
		$this -> db -> insert('question', $data);
		
		//recheck whether question is inserted successfully
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	function update_question($category_id, $content, $right_answer, $level, $modified_user, $note, $question_id)
	{
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$data = array(
			'category_id' => $category_id,
			'content' => $content,
			'right_answer' => $right_answer,
			'level' => $level,
			'note' => $note,
			'modified_user' => $modified_user,
			'modified_date' => date("Y/m/d H:i:s", time())
		);
		
		$this -> db -> where('question_id', $question_id);
		$this -> db -> update('question', $data);
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	function get_last_id()
	{
		return $this->db->insert_id();	
	}
	
	function delete_question($ques_id)
	{
		$this->db->delete('question', array('question_id' => $ques_id));
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;	
	}
	
	function get_total_question()
	{
		$this->db->select('*');	
		$this->db->from('question');
		
		$query = $this->db->get();
		
		if ($query)
			return $query->num_rows();
		else
			return 0;
	}
	
	function get_today_question()
	{
		$this->db->select('*');	
		$this->db->from('question');
		$this->db->where('date(created_date) = date(now()) or date(modified_date) = date(now())');
		
		$query = $this->db->get();
		
		if ($query)
			return $query->num_rows();
		else
			return 0;
	}
	
	function get_statistic_question()
	{
		$this->db->select('content, created_user, created_date, category_name');	
		$this->db->from('question');
		$this->db->join('category', 'category.category_id = question.category_id');
		$this->db->limit(5);
		
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
			return $query->result();
		else
			return NULL;
	}
}